/**
 * Created by yangzs on 2014-11-28.
 */
var ng = require('nodegrass');
var fs = require('fs');
var res;
var mysql = require('mysql');
var cookie;
var didata;
var endstr;
var s2;
var EventProxy = require('eventproxy');
var proxy = new EventProxy();

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '159456',
    database: 'yiqing'
});

//http://www.xooo8.com/AV/yazhouxingai/index1.html
var exec = require('child_process').exec;
var header = {'Cookie': cookie};
var s = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=7" /><link href="/pbl.css" rel="stylesheet" type="text/css" media="all" /><script src="http://liuxiaofan.com/demo/js/jquery-1.6.4.min.js" type="text/javascript"></script></head><body><div class="lxf-box"><ul>";
fs.writeFileSync('d:/temp/copy.txt', '', 'utf-8');
var bianma="utf-8";

function longzs(data){

    //var re = new RegExp("<li><a\\shref=\\\"(\\/htm\/[^<>]+\\.htm)\\\"","gi");
    //while (arr = re.exec(data)) {
    //    if (arr != null) {
    //        getlongzs("http://"+host + arr[1],function(data){
    //            endstr+=data+"\r\n";
    //        });
    //    }
    //}
}

function getlongzs(url,callback) {
    console.log(url);
    collecturl = "http://collect2.longsunhd.com/collect/index.php?gather_type=2&s_url=" + encodeURIComponent(url);
    ng.get(collecturl, function (data2, status, headers) {
        console.log("---" + collecturl)
        result = JSON.parse(data2);
        var re=/(http\:\/\/[^<>\s]+)/gi
        s2=""
        while(res = re.exec(result.message)){
            s2+="<li><a target=_bank href=\"/showlist.php?u="+encodeURIComponent(res[1])+"\"><img src=\""+res[1]+"\"></a></li>\r\n";

        }
        //callback(s2);
        //s2+="</ul></div></body></html><script src=\"/pbl.js\"></script>";
        //fs.writeFileSync('d:/test/index.php', s2, 'utf-8');
    }, header, 'utf-8').on('error', function (e) {
        console.log("Got error: " + e.message);
    });
}


function profile(s_url,nn){
        var hostre = new RegExp("http\\:\\/\\/([^\\/]+)\/");
        hostnn = s_url.match(hostre);
        var host = hostnn[1];
        if (nn != null) {
            for (var i = nn[1]; i <= nn[2]; i++) {
                var aurl = s_url.replace(nn[0], i);
                ng.get(aurl, function (data, status, headers) {
                    var listre = new RegExp("<li><a\\s+href=\\\"([^<>\\\"\\s]+)\\\"", "gi");
                    var urls = listre.exec(data);
                    while (urls = listre.exec(data)) {
                        collecturl = "http://collect2.longsunhd.com/collect/index.php?gather_type=2&s_url=http://" + host + encodeURIComponent(urls[1]);
                        ng.get(collecturl, function (data2, status, headers) {
                            result = JSON.parse(data2);
                            var re = /(http\:\/\/[^<>\s]+)/gi
                            while (res = re.exec(result.message)) {
                                s2 += "<li><img src=\"" + res[1] + "\"></li>\r\n";
                            }
                            proxy.emit("isend", s2);
                        }, header, 'utf-8').on('error', function (e) {
                            console.log("Got error: " + e.message);
                        });
                    }
                }, header, 'utf-8').on('error', function (e) {
                    console.log("Got error: " + e.message);
                });

            }
        }
}
//http://collect2.longsunhd.com:8080/collect/?s_url=http://www.339zz.com/htm/piclist9/[1-1].htm
s_url = "http://www.339zz.com/htm/piclist9/[1-1].htm";
collect=function(req,res) {
    var reg = new RegExp("\\[([0-9]+)\\-([0-9]+)\\]");
    if (reg.test(s_url)) {
        nn = reg.exec(s_url);
    }
    proxy.after("isend",(20*(nn[2]-1)),function (isend) {
        //console.log(isend[(isend.length-1)]);
        res.send(isend[(isend.length-1)]);
    });
    profile(s_url,nn);
}
var express=require('express');
var app=express();
app.get('/collect/*', collect);
app.listen(8080);
console.log("listen to 8080");



